import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.List;

import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.model.TimeZoneRegistry;
import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.model.property.Attendee;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Uid;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;


public class CalendarManager {
	
	private Calendar calendar;
	private String timeZone;
	
	public CalendarManager(String timeZone){
		this.timeZone = timeZone;
		this.calendar = new Calendar();
		calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
		calendar.getProperties().add(Version.VERSION_2_0);
		calendar.getProperties().add(CalScale.GREGORIAN);
	}
	
	public void createAllDayEvent(java.util.Calendar calendar, String text) throws SocketException{
		// Initialize as an all-day event..
		VEvent christmas = new VEvent(new Date(calendar.getTime()), text);
		
		// Generate a UID for the event..
		UidGenerator ug = new UidGenerator("1");
		christmas.getProperties().add(ug.generateUid());
		
		this.calendar.getComponents().add(christmas);
	}
	
	/**
	 * Create meeting
	 * 
	 * @param eventName
	 * @param startDate
	 * @param endDate
	 * @param attendees
	 * @throws SocketException
	 */
	public void createMeeting(String eventName, java.util.Calendar startDate, java.util.Calendar endDate, List<Attendee> attendees) throws SocketException{
		
		VTimeZone tz = getTimeZone();
		
		// Create the event
		DateTime start = new DateTime(startDate.getTime());
		DateTime end = new DateTime(endDate.getTime());
		VEvent meeting = new VEvent(start, end, eventName);
		
		// add timezone info..
		meeting.getProperties().add(tz.getTimeZoneId());
		
		// generate unique identifier..
		UidGenerator ug = new UidGenerator("uidGen");
		Uid uid = ug.generateUid();
		meeting.getProperties().add(uid);
		
		// add attendees..
		for(Attendee attendee : attendees){
			meeting.getProperties().add(attendee);
		}
		
		// Create a calendar
		this.calendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
		this.calendar.getProperties().add(CalScale.GREGORIAN);
		
		// Add the event
		this.calendar.getComponents().add(meeting);
		
	}
	
	private VTimeZone getTimeZone(){
		// Create a TimeZone
		TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
		TimeZone timezone = registry.getTimeZone(this.timeZone);
		VTimeZone tz = timezone.getVTimeZone();
		return tz;
	}
	
	/**
	 * 
	 * @return current calendar
	 */
	public Calendar getCalendar(){
		return this.calendar;
	}
	
	/**
	 * 
	 * @param name name of path
	 * @return file
	 * @throws IOException
	 * @throws ValidationException
	 */
	public FileOutputStream generateFileCalendar(String name) throws IOException, ValidationException{
		FileOutputStream fout = new FileOutputStream(name);

		CalendarOutputter outputter = new CalendarOutputter();
		outputter.output(this.calendar, fout);
		
		return fout;
	}

}
